package com.lovetemperature.action;

import com.lovetemperature.model.Member;
import com.lovetemperature.model.MemberState;
import com.lovetemperature.model.Sex;
import com.lovetemperature.service.MemberService;
import com.opensymphony.xwork2.ActionSupport;

public class OneMemberAction extends ActionSupport {
	private MemberService memberService;
	private Member member;
	private String memberId;
	private Sex[] candidateSexes = Sex.values();
	private MemberState[] candidateStates = MemberState.values();
	private String password;
	
	public String edit(){
		if(memberId!=null && !memberId.equalsIgnoreCase("")){	//修改已有
			member = memberService.findById(new Long(memberId));
		}else{	//新增
			member = new Member();
		}
		memberId = null;
		return "success";
	}
	
	public String submit(){
		if(memberId!=null && !memberId.equalsIgnoreCase("")){	//修改已有
			finishPassword();
			memberService.update(member);
		}else{	//新增
			memberService.addMember(member);
			member = new Member();
		}
		memberId = null;
		return "success";
	}
	
	private void finishPassword(){
		if(password!=null && !password.equalsIgnoreCase("")){
			member.setPassword(password);
		}
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getMemberId() {
		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public Sex[] getCandidateSexes() {
		return candidateSexes;
	}

	public void setCandidateSexes(Sex[] candidateSexes) {
		this.candidateSexes = candidateSexes;
	}

	public MemberState[] getCandidateStates() {
		return candidateStates;
	}

	public void setCandidateStates(MemberState[] candidateStates) {
		this.candidateStates = candidateStates;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
